// Angular Version 6 below need to be deleted {providedIn: 'root'}

// angular
import {Injectable} from '@angular/core';
import {HttpParams} from '@angular/common/http';
// owner
import {Request} from 'pengesoft-ng-lib';
// models

import { LoginResult } from '../domains/loginResult.domain';
/**
 * IAuthorizeSvr 接口定义，该接口必须用Https发布。
 *
 */
@Injectable({
  providedIn: 'root'
})
export class Login {
  private readonly baseUrl: string;

  constructor(
    private request: Request
  ) {
    this.baseUrl = 'Authorize.assx/';
  }

  /**
   * 用户登录, 带自定义验证信息
   *
   * @param loginId    用户登录标识(nickName/mob/qq/email)
   * @param password   口令(明文)
   * @param userDefTag 用户写定义验证标识, 不使用标识时传入空
   * @return 成功返回用户登录标识(token不为空), 失败返回 Code和Msg.
   * @throws IPSFNetException
   * @throws Exception
   */
  Login(loginId: string, password: string, userDefTag: string): Promise<LoginResult> {
    const httpParams = new HttpParams()
      .append('loginId', loginId.toString())
      .append('password', password.toString())
      .append('userDefTag', userDefTag.toString());
    return this.request.post<LoginResult>(this.baseUrl + 'Login', httpParams).then((res) => {
      return new LoginResult(res);
    });
  }


}
